package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.Date;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseApprovalRecords;
import top.jnaw.jee.platform.sample.ModelX;
import top.jnaw.jee.utils.ModelResult;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class ApprovalRecords extends BaseApprovalRecords<ApprovalRecords> {

  public static final ApprovalRecords dao = new ApprovalRecords().dao();

  public static ModelResult add(int aid, JSONArray process) {
    ModelResult result = new ModelResult(Result.kBadParam);

    if (aid >= 0 && null != process) {
      for (int i = 0; i < process.size(); ++i) {
        JSONObject stage = process.getJSONObject(i);
        JSONArray approvers = stage.getJSONArray(Consts.JSON_RES_APPROVERS);
        int minimumPass = stage.getInteger(Consts.JSON_RES_MINI_PASS);
        int count = approvers.size();

        for (int j = 0; j < count; ++j) {
          ApprovalRecords record = new ApprovalRecords();

          record.setAid(aid);
          record.setMinimumPass(minimumPass);
          record.setApprover(approvers.getInteger(j));
          record.setStage(i + 1);
          ModelX.save(record, result);

          if (result.fine()) {
            result.setResult(Result.kSuccess);
          } else {
            result.setResult(Result.kModelSaveError);
          }
        }
      }
    }

    return result;
  }

  public static boolean add(int aid, int approver, int stage, int minimumPass) {
    boolean r = false;
    if (aid > 0 && approver > 0) {
      ApprovalRecords records = new ApprovalRecords();
      records.setAid(aid);
      records.setStatus(Consts.APPROVAL_CHECKING);
      records.setApprover(approver);
      records.setStage(stage);
      records.setMinimumPass(minimumPass);
      r = records.save();
    }
    return r;
  }

  public static List<ApprovalRecords> list(int aid, int stage) {
    String sql = Db.getSql("approvalRecords-list-stage");
    return dao.find(sql, aid, stage);
  }

  public static ModelResult update(int approver, int aid, int stage,
      String status, String content) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);

    if ("cancel".endsWith(status)) {
      List<ApprovalRecords> list = dao
          .find(Db.getSql("approvalRecords-By-Aid"), aid);
      if (null != list && list.size() > 0) {
        for (ApprovalRecords records : list) {
          records.setStatus(Consts.APPROVAL_CANCEL);
          records.setLastModifyTime(new Date());
          records.update();
          result.setObject(records);
        }
        result.setResult(Result.kSuccess);
      }
    } else {
      String sql = Db.getSql("approvalRecords-update-approval");

      ApprovalRecords record = dao
          .findFirst(sql, aid, stage, approver);

      if (null != record) {
        record.setStatus(status);
        record.setLastModifyTime(new Date());

        if (StrKit.notBlank(content)) {
          record.setContent(content);
        }

        record.update();

        int min = record.getMinimumPass();

        result = status(aid, stage);
        JSONObject r = (JSONObject) result.getObject();
        Integer passed = r.getInteger(Consts.APPROVAL_PASSED);
        Integer rejected = r.getInteger(Consts.APPROVAL_REJECTED);

        if (null != passed && passed >= min) {
          update(aid, stage, Consts.APPROVAL_PASSED);
        } else if (null != rejected) {
          update(aid, stage, Consts.APPROVAL_REJECTED);
        }
        result.setObject(record);
        result.setResult(Result.kSuccess);
      }
    }

    return result;
  }

  public static ModelResult status(int aid, int stage) {
    ModelResult result = new ModelResult();

    String sql = Db.getSql("approvalRecords-status");

    List<Record> list = Db.find(sql, aid, stage);

    JSONObject object = new JSONObject();
    for (Record r : list) {
      object.put(r.getStr("status"), r.getInt("count"));
    }
    result.setObject(object);
    result.setResult(Result.kSuccess);

    return result;
  }

  public static ModelResult update(int aid, int stage, String status) {
    ModelResult result = new ModelResult(Result.kNull);

    String sql = Db.getSql("approvalRecords-update");

    int rows = Db.update(sql, status, aid, stage);
    if (rows > 0) {
      result.setResult(Result.kSuccess);
    }

    return result;
  }

  public static List<ApprovalRecords> getList(int aid) {
    String sql = Db.getSql("approvalRecords-listForPush");

    return dao.find(sql, aid);
  }
}
